package com.bupt.datacollect.service;

import com.bupt.datacollect.data.Mapping;
import com.bupt.datacollect.data.Packet;
import com.bupt.datacollect.dto.Energy;
import com.bupt.datacollect.dto.PacketDTO;
import com.bupt.datacollect.mapper.PacketListMapper;
import com.bupt.datacollect.util.Consts;
import com.bupt.datacollect.util.NumberFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class PacketListService {

    @Autowired
    private PacketListMapper packetListMapper;

    public List<PacketDTO> list(HttpServletRequest request, Integer start, Integer limit){
        String id=request.getParameter("id");
        if (id!=null&&!"".equals(id)){
            String meterId=packetListMapper.getMeterId(id);
            if (meterId!=null) id=meterId;
        }
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String alarmType=request.getParameter("alarmType");
        String alertType=null,validatedType=null;
        if (alarmType!=null&&!"".equals(alarmType)){
            if (alarmType.equals("1")){
                alertType=alarmType;
            }else if (alarmType.equals("2")){
                validatedType=alarmType;
            }
        }
        List<PacketDTO> list=new ArrayList<>();
        List<Packet> packets= packetListMapper.packetList(id,startTime,endTime,alertType,validatedType,start,limit);
        List<Mapping> mappings=packetListMapper.getMapping();
        Map<Integer,String> map=new HashMap<>();
        mappings.forEach(o->{
            map.put(o.getMeterId(),o.getCarId());
        });
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        packets.forEach(o->{
            PacketDTO dto=new PacketDTO();
            BeanUtils.copyProperties(o,dto);
            dto.setStatus(o.getValidated()==0?"正常":"异常");
            dto.setTime(format.format(o.getTime()));
            dto.setPower(NumberFormat.formatFloat(o.getPower()/1000,2));
            dto.setEnergyConsumption(o.getEnergyConsumption()/3600000);
            dto.setCurrent(NumberFormat.formatFloat(o.getCurrent(),2));
            if (map.containsKey(o.getMeterId())){
                dto.setCarId(map.get(o.getMeterId()));
            }else {
                dto.setCarId(o.getMeterId().toString());
            }
            StringBuilder alarmStatus=new StringBuilder();
            if (o.getAlertFirst()==1){
                alarmStatus.append("第一个100ms异常");
            }
            if (o.getAlertSecond()==1){
                alarmStatus.append(" 第二个100ms异常");
            }
            if (o.getPower()>= Consts.Alarm_Power){
                alarmStatus.append(" 瞬时功率大于80kw");
            }
            if (o.getAlertFirst()!=1&&o.getAlertSecond()!=1&&o.getPower()<Consts.Alarm_Power){
                alarmStatus.append("正常");
            }
            dto.setAlarmStatus(alarmStatus.toString());
            list.add(dto);
        });
        return list;
    }

    public Integer getTotalRecords(HttpServletRequest request){
        String id=request.getParameter("id");
        if (id!=null&&!"".equals(id)){
            id=id.trim();
            id=packetListMapper.getMeterId(id);
            String meterId=packetListMapper.getMeterId(id);
            if (meterId!=null) id=meterId;
        }
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String alarmType=request.getParameter("alarmType");
        String alertType=null,validatedType=null;
        if (alarmType!=null&&!"".equals(alarmType)){
            if (alarmType.equals("1")){
                alertType=alarmType;
            }else if (alarmType.equals("2")){
                validatedType=alarmType;
            }
        }
        return packetListMapper.getTotalRecords(id,startTime,endTime,alertType,validatedType);
    }

    public List<Packet> meterlist(HttpServletRequest request,Integer start,Integer limit){
        String id="1";
        return packetListMapper.metetPacketList(id,start,limit);
    }

    public Integer getMeterTotalRecords(HttpServletRequest request){
        String id="1";
        return packetListMapper.getMeterTotalRecords(id);
    }

    public List<Energy> eneryTotal(Integer id){
        return packetListMapper.getEneryTotal(id);
    }

    public List<PacketDTO> checkAlarm(){
        List<Packet> packets=packetListMapper.checkAlarm();
        List<Mapping> mappings=packetListMapper.getMapping();
        Map<Integer,String> map=new HashMap<>();
        mappings.forEach(o->{
            map.put(o.getMeterId(),o.getCarId());
        });
        List<PacketDTO> list=new ArrayList<>();
        packets.forEach(o->{
            PacketDTO dto=new PacketDTO();
            BeanUtils.copyProperties(o,dto);
            if (map.containsKey(o.getMeterId())){
                dto.setCarId(map.get(o.getMeterId()));
            }else {
                dto.setCarId(o.getMeterId().toString());
            }
            list.add(dto);
        });
        return list;
    }

    public List<PacketDTO> lastestList( Integer start, Integer limit){
        List<PacketDTO> list=new ArrayList<>();
        List<Packet> packets= new ArrayList<>();
        List<Mapping> mappings=packetListMapper.getMapping();
        List<Integer> ids=packetListMapper.lastestId(start,limit);
        ids.forEach(o->{
            Packet packet=packetListMapper.getPacketById(o);
            packets.add(packet);
        });
        Map<Integer,String> map=new HashMap<>();
        mappings.forEach(o->{
            map.put(o.getMeterId(),o.getCarId());
        });
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        packets.forEach(o->{
            PacketDTO dto=new PacketDTO();
            BeanUtils.copyProperties(o,dto);
            dto.setStatus(o.getValidated()==0?"正常":"异常");
            dto.setTime(format.format(o.getTime()));
            dto.setPower(NumberFormat.formatFloat(o.getPower()/1000,2));
            dto.setEnergyConsumption(o.getEnergyConsumption()/3600000);
            dto.setCurrent(NumberFormat.formatFloat(o.getCurrent(),2));
            if (map.containsKey(o.getMeterId())){
                dto.setCarId(map.get(o.getMeterId()));
            }else {
                dto.setCarId(o.getMeterId().toString());
            }
            StringBuilder alarmStatus=new StringBuilder();
            if (o.getAlertFirst()==1){
                alarmStatus.append("第一个100ms异常");
            }
            if (o.getAlertSecond()==1){
                alarmStatus.append(" 第二个100ms异常");
            }
            if (o.getPower()>= Consts.Alarm_Power){
                alarmStatus.append(" 瞬时功率大于80kw");
            }
            if (o.getAlertFirst()!=1&&o.getAlertSecond()!=1&&o.getPower()<Consts.Alarm_Power){
                alarmStatus.append("正常");
            }
            dto.setAlarmStatus(alarmStatus.toString());
            list.add(dto);
        });
        return list;
    }

}
